/**
 * author: lms
 */
const STORE_KEY = 'r-boot-table-store'

const RBootTableStoreHelper = {
  store(key, objectList) {
    let store = JSON.parse(localStorage.getItem(STORE_KEY))
    if (store === null) {
      store = {}
    }
    store[key] = []
    for (let object of objectList) {
      store[key].push({
        ifShow: object.ifShow,
        label: object.label,
        fixed: object.fixed
      })
    }

    localStorage.setItem(STORE_KEY, JSON.stringify(store))
  },
  fetch(key) {
    let store = JSON.parse(localStorage.getItem(STORE_KEY))

    // 兼容旧版
    if(store === null) {
      store = JSON.parse(localStorage.getItem('rw-table-store'))
      localStorage.setItem(STORE_KEY, JSON.stringify(store))
    }

    if (store) {
      return store[key]
    }
    return null
  },
  clear() {
    localStorage.removeItem(STORE_KEY)
  }
}

export default RBootTableStoreHelper
